<?php

if (preg_match('/^[http|https]+[:\/\/]+[A-Za-z0-9\-_]+\\.+[#~A-Za-z0-9\.\/%&=\?\-_:\+]+$/i',$_GET['url'])) {

  $url = htmlentities($_GET['url']);
  $urlFwd = urlencode($_GET['url']);
  $raw = htmlentities($_GET['raw']);

  // URL fetching
  $curl_handle = curl_init();
  curl_setopt($curl_handle,CURLOPT_URL,$url);
  curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
  curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
  curl_setopt($curl_handle,CURLOPT_REFERER,'');
  curl_setopt($curl_handle,CURLOPT_USERAGENT,'');
  $buffer = curl_exec($curl_handle);
  $status = curl_getinfo($curl_handle,CURLINFO_HTTP_CODE);
  curl_close($curl_handle);

  // DOM queries
  $dom = new DomDocument();
  @$dom->loadHTML($buffer);

  // Element counter
  function countElements($elementName) {
    global $dom;
    $element = $dom->getElementsByTagName($elementName);
    $elements = $element->length;
    return $elements;
  }

  // Nested table elements
  $tables = $dom->getElementsByTagName('table');
  $tableCounter = 0;
  foreach ($tables as $table) {
    $table = $table->getElementsByTagName('table');
    if ($table->length) {
      $tableCounter++;
    }
  }

  // Logging
  if (preg_match('/^[4]/',$status) && ($raw != 'yes')) {
    print '<h3>Results for, uhm, yes</h3>';
    print '<p>Something went wrong. You sure I’ve got a nice URL to test?';
  } else if ($raw != 'yes') {
    // print $buffer;
    echo '<h3>Results for <a href="http://www.google.com/url?sa=D&amp;q=' , $urlFwd , '" target="_blank"><code>' , $url , '</code></a> (<a href="index.php5">clear</a>)</h3>';
    echo '<ul>';
    echo '<li><code>h1</code> elements: ' , countElements('h1');
    echo '<li><code>h2</code> elements: ' , countElements('h2');
    echo '<li><code>h3</code> elements: ' , countElements('h3');
    echo '<li><code>h4</code> elements: ' , countElements('h4');
    echo '<li><code>h5</code> elements: ' , countElements('h5');
    echo '<li><code>h6</code> elements: ' , countElements('h6');
    echo '</ul><ul>';
    // echo '<li><code>applet</code> elements: ' , substr_count(strtolower($buffer),'<applet');
    echo '<li><code>applet</code> elements: ' , countElements('applet');
    echo '<li><code>basefont</code> elements: ' , countElements('basefont');
    echo '<li><code>center</code> elements: ' , countElements('center');
    echo '<li><code>dir</code> elements: ' , countElements('dir');
    echo '<li><code>font</code> elements: ' , countElements('font');
    echo '<li><code>isindex</code> elements: ' , countElements('isindex');
    echo '<li><code>menu</code> elements: ' , countElements('menu');
    echo '<li><code>s</code> elements: ' , countElements('s');
    echo '<li><code>spacer</code> elements: ' , countElements('spacer');
    echo '<li><code>strike</code> elements: ' , countElements('strike');
    echo '<li><code>u</code> elements: ' , countElements('u');
    echo '</ul><ul>';
    echo '<li>Nested <code>table</code>s: ' , $tableCounter;
    echo '</ul>';
    echo '<p class="twisty">Page source';
    echo '<div id="buffer">' , htmlspecialchars($buffer) , '</div>';
  }

  // Scoring
  if ((countElements('h1') + countElements('h2') + countElements('h3') + countElements('h4') + countElements('h5') + countElements('h6')) == 0) {
    $headings = 3;
  } else {
    $headings = 0;
  }
  $badboys = (countElements('applet') + countElements('basefont') + countElements('center') + countElements('dir') + countElements('font') + countElements('isindex') + countElements('menu') + countElements('s') + countElements('spacer') + countElements('strike') + countElements('u'));
  if ($tableCounter > 0) {
    $cssunknown = 1;
  } else {
    $cssunknown = 0;
  }

  $scoreTemp = (10 - $headings - $badboys - $cssunknown);
  if ($scoreTemp < 0) {
    $score = 0;
  } else {
    $score = $scoreTemp;
  }
  if (!preg_match('/^[4]/',$status)) {
    if ($raw != 'yes') {
    echo '<p><strong>Score:</strong> ';
    }
    if ($score < 0) {
      echo '0';
    } else {
      echo $score;
    }
    if ($raw != 'yes') {
      echo '/10 (<a href="check.php5?url=' , $urlFwd , '&amp;raw=yes" target="_blank">raw</a>)';
      // echo '<p><a href="./">Test another page.</a>';
    }
  }

} else if (htmlentities($_GET['url']) && ($raw != 'yes')) {

  print '<h3>Results for, uhm, yes</h3>';
  print '<p>Something went wrong. You sure I’ve got a nice URL to test?';

}

?>